﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using RainEngine.SceneManagement;

namespace RainEngine.SceneNodes.UI
{
    /// <summary>
    /// 文本标签
    /// </summary>
    public class UILabel : UISceneNode
    {
        #region 成员变量和构造函数

        /// <summary>
        /// 文字
        /// </summary>
        protected string text;        

        /// <summary>
        /// 文字使用的字体
        /// </summary>
        protected SpriteFont font; 

        /// <summary>
        /// 文字颜色
        /// </summary>
        protected Color fontColor;

        /// <summary>
        /// 创建一个新UILabel对象，默认文字为"Label"，默认颜色为白色，默认字体为引擎的DefaultFont     
        /// </summary>
        /// <param name="engine">引擎</param>
        /// <param name="setScene">所属场景</param>        
        public UILabel(RainGE engine, Scene setScene)
            : this(engine, setScene, "Label", Vector2.Zero, Color.White, engine.DefaultFont)
        {
            
        }

        /// <summary>
        /// 创建一个新UILabel对象，内部方法，只能在引擎内部调用        
        /// </summary>
        /// <param name="engine">引擎</param>
        /// <param name="setScene">所属场景</param>
        /// <param name="setText">文字</param>
        /// <param name="setPosition">文字的2D屏幕位置</param>
        /// <param name="setTextColor">文字颜色</param>
        /// <param name="setFont">字体</param>
        /// <param name="position">2D屏幕位置</param>
        /// <param name="font">字体</param>
        internal UILabel(RainGE engine, Scene setScene, string setText, Vector2 setPosition, Color setTextColor, SpriteFont setFont)
            : base(engine, setScene, setPosition, null,false )
        {
            this.Name =this.text = setText;
            this.fontColor =setTextColor ;
            this.font=setFont ;
            this.size = new Vector2 (font.MeasureString(setText).X, font.MeasureString(setText).Y);
            this.TabStop = false;
        }

        #endregion

        #region 属性
        
        /// <summary>
        /// 获取或设置文字
        /// </summary>
        public string Text 
        {
            get { return text; } 
            set 
            {
                text = value; 
                //改变文字后需要重新设置文本标签大小
                this.size = new Vector2 (font.MeasureString(text).X, font.MeasureString(text).Y); 
            }
        }

        /// <summary>
        /// 获取或设置文字使用的字体
        /// </summary>
        public SpriteFont Font
        {
            get { return font; }
            set { 
                font = value;
                //改变字体后需要重新设置文本标签大小
                this.size = new Vector2(font.MeasureString(text).X, font.MeasureString(text).Y);
            }
        }

        /// <summary>
        /// 获取或设置文字的颜色
        /// </summary>
        public Color FontColor
        {
            get { return fontColor; }
            set { fontColor = value; }
        }        

        #endregion

        /// <summary>
        /// 绘制文本标签
        /// </summary>
        /// <param name="gameTime"></param>       
        public override int Draw(GameTime gameTime)
        {

            // 设置文字淡入淡出的透明度
            Color alphaTextColor = new Color(fontColor.R, fontColor.G, fontColor.B, Scene.TransitionAlpha);
            // 绘制文字
            RainGE.SpriteBatch.DrawString(font, text, new Vector2(Position.X, Position.Y), alphaTextColor, Rotation, Origin, 1.0f, SpriteEffect, LayerDepth);
            return 1;

        }        
    }
}
